<template>
  <!-- 宠物页下的二级切换数据 -->
  <van-sticky :offset-top="30">
    <div class="page_chongwu_text_body">
      <!-- 自定义区域：当 hideTabs 为 true 时显示 -->
      <!-- <div class="custom-region">
        <div
          class="region_item"
          v-for="(item, index) in RecommendData"
          :key="index"
          :class="{ active: customActiveIndex === index }"
          @click="setCustomActive(index)"
        >
          <span>{{ item.RecommendData_text }}</span>
        </div>
      </div> -->
      <van-tabs v-model:active="active" animated>
        <!-- 5. 普通访问时显示服务Tab -->
        <van-tab class="van_switch_first" title="On Sale">
          <div class="van_switch_first_nav">
            <div
              class="van_switch_first_nav_item"
              v-for="(item, index) in petCareAll"
              :key="index"
              :class="{ active: petCareAllindex === index }"
              @click="petCareAllindex = index"
            >
              <span>{{ item.title }}</span>
            </div>
          </div>
          <div class="van_switch_first_list">
            <van-card
              v-for="(item, index) in cardFirstData"
              :key="index"
              :desc="item.cardFirst_desc"
              :title="item.cardFirst_title"
              :thumb="item.cardFirst_image"
              :style="{
                '--origin-price': `'${item.cardFirst_originPrice}'`,
              }"
            >
              <template #footer>
                <van-button
                  @click="
                    BookFn(
                      cardFirstData.length,
                      item.cardFirst_title,
                      item.cardFirst_image
                    )
                  "
                  round
                  color="#1ab692"
                  >BOOK</van-button
                >
              </template>
            </van-card>
          </div>
        </van-tab>
        <!-- 切换栏第二个展示 -->
        <!-- 6. 始终显示评分Tab -->
        <van-tab class="van_switch_last" title="Ratings">
          <div class="van_switch_last_list">
            <div
              class="van_switch_last_list_item"
              v-for="(item, index) in van_Switch_Last_List"
              :key="index"
            >
              <div class="last_list_item_hd">
                <div class="hd_l">
                  <img :src="item.userimage" alt="" />
                  <div class="username">
                    <span>{{ item.username }}</span>
                  </div>
                </div>
                <div class="hd_r">
                  <span>{{ item.usertimer }}</span>
                </div>
              </div>
              <!-- 评分 -->
              <van-rate
                v-model="item.userrate"
                :size="12"
                color="#ffd21e"
                void-icon="star"
                void-color="#eee"
              />
              <!-- 评论 -->
              <div class="last_list_item_bd">
                <p>{{ item.usertext }}</p>
              </div>
              <!-- 点赞 -->
              <div
                class="last_list_item_ft"
                @click="like(index)"
                :style="{ color: item.userlike ? '#fe6b00' : '' }"
              >
                <van-icon
                  size="20"
                  :name="item.userlike ? 'good-job' : 'good-job-o'"
                  :color="item.userlike ? '#fe6b00' : ''"
                />
                <p>helpful({{ van_Switch_Last_List[index].likeCount }})</p>
                <span style="color: initial">···</span>
              </div>
            </div>
          </div>
        </van-tab>
      </van-tabs>
    </div>
  </van-sticky>
</template>

<script setup>
import { ref, watch } from "vue";
import { useRouter, useRoute } from "vue-router";
const route = useRoute();
const router = useRouter();
// 切换二内容的数据
import user1 from "@/assets/user1.png";
import user2 from "@/assets/user2.png";
import user3 from "@/assets/user3.png";

const props = defineProps(["forceRatings"]);

const petCareAllindex = ref(0); // 默认为第一个选中 All
const petCareAll = ref([
  {
    title: "All",
  },
  {
    title: "Service",
  },
  {
    title: "Shop",
  },
]);
const RecommendData = ref([
  {
    RecommendData_text: "Recommended",
  },
  {
    RecommendData_text: "Buy again",
  },
]);
import dog1 from "../assets/dog1.png";
import dog2 from "../assets/dog2.png";
import dog3 from "../assets/dog3.png";
import dog4 from "../assets/dog4.png";

const cardFirstData = [
  {
    cardFirst_image: dog1,
    cardFirst_title: "Dog Vaccline",
    cardFirst_desc: "C$99",
    cardFirst_originPrice: "C$149",
  },
  {
    cardFirst_image: dog2,
    cardFirst_title: "Pet Physical Examination",
    cardFirst_desc: "C$80",
    cardFirst_originPrice: "C$120",
  },
  {
    cardFirst_image: dog3,
    cardFirst_title: "Pet insect repellent with a long trade name",
    cardFirst_desc: "C$280",
    cardFirst_originPrice: "C$369",
  },
  {
    cardFirst_image: dog4,
    cardFirst_title: "Pet insect repellent with a long trade name",
    cardFirst_desc: "C$280",
    cardFirst_originPrice: "C$369",
  },
];
// 切换二内容的数据
// 点赞信息
const like = (index) => {
  const item = van_Switch_Last_List.value[index];
  item.userlike = !item.userlike;
  item.likeCount = String(parseInt(item.likeCount) + (item.userlike ? 1 : -1));
};

const van_Switch_Last_List = ref([
  {
    userimage: user1,
    username: "Jo****",
    usertimer: "17 July 2023",
    userrate: 5,
    usertext:
      "  This is a comment return for is a comment order free from   the same order,shipping and handling  comment return for is comment order free from the shipping",
    userlike: false,
    likeCount: "34",
  },
  {
    userimage: user2,
    username: "Me****",
    usertimer: "17 July 2023",
    userrate: 4,
    usertext:
      "  This is a comment return for is a comment order free from   the same order,shipping and handling  comment return for is comment order free from the shipping",
    userlike: true,
    likeCount: "163",
  },
  {
    userimage: user3,
    username: "Ca****",
    usertimer: "17 July 2023",
    userrate: 5,
    usertext:
      "  This is a comment return for is a comment order free from   the same order,shipping and handling  comment return for is comment order free from the shipping",
    userlike: false,
    likeCount: "34",
  },
  {
    userimage: user1,
    username: "J0****",
    usertimer: "17 July 2023",
    userrate: 5,
    usertext:
      "  This is a comment return for is a comment order free from   the same order,shipping and handling  comment return for is comment order free from the shipping",
    userlike: true,
    likeCount: "34",
  },
]);
// 动态设置第二个切换栏的标题

const active = ref(0); // 默认选中第一个 Tab

// 自定义区域的选中状态
const customActiveIndex = ref(0); // 默认选中第一个

// 设置自定义区域的选中状态
const setCustomActive = (index) => {
  customActiveIndex.value = index;
};

const BookFn = (length, title, image) => {
  router.push({
    name: "petBook",
    query: {
      length: length,
      title: title,
      image: image,
    },
  });
};
</script>
<style lang="scss">
.page_chongwu_text_body {
  padding: 10px;

  .van-tabs {
    .van-tabs__wrap {
      .van-tabs__nav {
        background-color: transparent;
        .van-tab {
          flex: none;
        }
        .van-tabs__line {
          width: 45px;
          height: 5px;
          bottom: 25px;
          background-color: #17b68d;
        }
      }
    }
    .van-tabs__content {
      padding-bottom: 100px; /* 确保底部有足够空间 */
      overflow-y: auto; /* 允许滚动 */
      max-height: calc(100vh - 200px); /* 减去 sticky 高度和顶部偏移 */
      // 切换栏第一个展示的样式
      .van_switch_first {
        .van_switch_first_nav {
          display: flex;
          align-items: center;
          margin: 0 auto;
          overflow-x: auto;
          scrollbar-width: none; /* 针对 Firefox 浏览器隐藏滚动条 */
          -ms-overflow-style: none; /* 针对 IE/Edge 浏览器隐藏滚动条 */
          padding: 6px 0;
          &::-webkit-scrollbar {
            display: none; /* 针对 Chrome/Safari/Opera 浏览器隐藏滚动条 */
          }
          .van_switch_first_nav_item {
            background-color: #e5e7eb;
            color: #6c7078;
            border-radius: 15px;
            margin-left: 8px;
            white-space: nowrap;
            text-align: center;
            line-height: 30px;

            span {
              padding: 12px;
              font-size: 12px;
              font-weight: bold;
            }
            &:first-child {
              margin-left: 0;
            }
            &.active {
              background-color: #c9f5ed;
              color: #35b590;
            }
          }
        }
        .van_switch_first_list {
          .van-card {
            background-color: #fff;
            padding: 0;
            border-radius: 10px;
            position: relative;
            box-sizing: border-box;

            .van-card__header {
              align-items: center;
              margin-top: 15px;
              padding: 6px;
              box-sizing: border-box;
              .van-card__thumb {
                width: 70px;
                height: 70px;
                img {
                  border-radius: 10px;
                }
              }
              .van-card__title {
                width: 74%;
                margin-top: 10px;
                font-size: 15px;
              }
              .van-card__desc {
                color: #fb6f15;
                font-size: 13px;
                margin-top: 6px;
                position: relative;
                &::before {
                  content: var(--origin-price); /* 使用 CSS 变量 */
                  position: absolute;
                  top: 50%;
                  left: 50px;
                  color: #9fa3a7;
                  text-decoration: line-through; /* 如果需要删除线效果 */
                  transform: translateY(-50%);
                  font-size: 12px; /* 可选：调整字体大小 */
                }
              }
            }
            .van-card__footer {
              position: absolute;
              top: 10px;
              right: 10px;
              .van-button {
                width: 60px;
                height: 35px;
              }
            }
            // 只针对前两个卡片 desc 的伪元素
            &:nth-child(-n + 2) .van-card__desc::before {
              /* 覆盖样式 */
              left: 35px; /* 调整位置 */
            }
          }
        }
      }
      // 切换栏第二个展示的样式
      .van_switch_last {
        .van_switch_last_list {
          .van_switch_last_list_item {
            background-color: #fff;
            border-radius: 10px;
            padding: 10px;
            margin-top: 15px;

            .last_list_item_hd {
              display: flex;
              align-items: center;
              justify-content: space-between;
              .hd_l {
                display: flex;
                align-items: center;
                font-size: 16px;

                img {
                  width: 30px;
                  height: 30px;
                  border-radius: 50%;
                  margin-right: 4px;
                }
              }
              .hd_r {
                font-size: 12px;
                color: #ccc;
              }
            }
            .last_list_item_bd {
              font-size: 12px;
              color: #888e95;
              line-height: 14px;
            }
            .last_list_item_ft {
              height: 40px;
              display: flex;
              align-items: center;
              justify-content: end;
              font-size: 14px;

              p {
                margin: 0 8px;
              }
            }
          }
        }
      }
    }
  }
}
/* 隐藏 van-tabs 的标题栏 */
.hidden-tabs .van-tabs__wrap {
  display: none;
}
/* 自定义区域样式 */
.custom-region {
  display: flex;
  margin-top: 24px;
  padding-bottom: 12px;
  .region_item {
    font-size: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #595e65;
    background-color: #e6e6eb;
    padding: 6px 20px;
    border-radius: 16px;
    position: relative;
    &:first-child {
      margin-right: 12px;
    }
    &.active {
      background-color: #caf4ed;
      color: #1fb68d;
      &::before {
        // 向下箭头
        content: "";
        position: absolute;
        right: 8px;
        width: 5px;
        height: 5px;
        border: 2px solid #43c8a2;
        border-top: 0;
        border-left: 0;
        transform: translateY(-80%) rotate(45deg);
        top: 50%;
      }
    }
  }
}
</style>